/**
 * 
 */
package com.iss.card.view.collent;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.iss.card.R;
import com.iss.card.cache.BitmapConfig;
import com.iss.card.cache.ImageDataManager;
import com.iss.card.common.ApplicationManager;
import com.iss.card.model.MarkedProductDetailInfo;
import com.iss.card.model.ProductDetailInfo;

/**
 * @author dtdh
 * 
 */
public class MarkedProductListAdapter extends BaseAdapter {
	private static final String TAG = MarkedProductListAdapter.class
			.getSimpleName();
	private Context mContext;
	private int mColumnNum = 0;
	private int mColumnWidth = 0;
	private SparseArray<List<MarkedProductDetailInfo>> mContentList = null;

	/**
	 * 
	 */
	public MarkedProductListAdapter(Context context) {
		mContext = context;
	}

	public void setColumnCount(int columnum) {
		mColumnNum = columnum;
		mColumnWidth = ApplicationManager.SCREEN_WIDTH / mColumnNum;
		mContentList = new SparseArray<List<MarkedProductDetailInfo>>(
				mColumnNum);
	}

	public synchronized void setContents(
			SparseArray<List<MarkedProductDetailInfo>> list) {
		mContentList.clear();
		final int num = list.size();
		if (num != mColumnNum) {
			try {
				throw new Exception("invalid parameter, the size(" + num
						+ ") should be equal to (" + mColumnNum
						+ ") which set by call setColumnCount()");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		for (int i = 0; i < num; ++i) {
			int key = list.keyAt(i);
			List<MarkedProductDetailInfo> tmp = new ArrayList<MarkedProductDetailInfo>();
			tmp.addAll(list.get(key));
			mContentList.put(key, tmp);
		}
		this.notifyDataSetChanged();
	}

	public int getColumnCount() {
		return mColumnNum;
	}

	public int getRowCount(int column) {
		return mContentList.get(column).size();
	}

	public Object getRowItem(int column, int row) {
		return mContentList.get(column).get(row);
	}

	public View getView(int column, int row, View convertView, ViewGroup parent) {
		ViewHolder hv = null;
		if (convertView == null) {
			hv = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.collect_view_marked_product_item, null);
			hv.src = (ImageView) convertView
					.findViewById(R.id.iv_collect_view_marked_product_item);
			convertView.setTag(hv);
		} else {
			hv = (ViewHolder) convertView.getTag();
		}

		final ProductDetailInfo info = mContentList.get(column).get(row);
		final String url = info.getProductPicUrl();
		Log.i(TAG, "column = " + column + ", row=" + row + "; url = " + url);

		hv.src.setTag(url);
//		ImageDataManager.instance().loadBitmap(
//				url,
//				hv.src,
//				new BitmapConfig(mColumnWidth, (info.getProductPicH()
//						* mColumnWidth / info.getProductPicW())));

		return convertView;
	}

	public SparseArray<List<MarkedProductDetailInfo>> getContents() {
		return mContentList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {
		final int num = mContentList.size();
		int total = 0;
		for (int i = 0; i < num; ++i) {
			total += mContentList.get(i).size();
		}
		return total;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public Object getItem(int position) {
		return mContentList.get(position);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int position) {
		return position;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getView(int, android.view.View,
	 * android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// ViewHolder hv = null;
		// if (convertView == null) {
		// hv = new ViewHolder();
		// convertView = LayoutInflater.from(mContext).inflate(
		// R.layout.collect_view_marked_product_item, null);
		// hv.src = (ImageView) convertView
		// .findViewById(R.id.iv_collect_view_marked_product_item);
		// convertView.setTag(hv);
		// } else {
		// hv = (ViewHolder) convertView.getTag();
		// }
		//
		// final ProductDetailInfo info = mContentList.get(position);
		// final String url = info.getProductPicUrl();
		// Log.i(TAG, "position = " + position + ";url = " + url);
		//
		// hv.src.setTag(url);
		// ImageDataManager.instance().loadBitmap(url, hv.src);

		return convertView;
	}

	private final class ViewHolder {
		public ImageView src;
	}
}
